package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.entity.Employee;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    //自动注入员工的service
    @Autowired
    private EmployeeService employeeService;

    //登录方法
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        //编写登录方法
        //1.对密码进行MD5加密
            String password= employee.getPassword();
            String username= employee.getUsername();
            //加密
        String MD5password= DigestUtils.md5DigestAsHex(password.getBytes());
        //2.用用户名查询用户对象是不是存在  存在 进行第三步   不存在登录失败

        //new  一个lambdaQueryWarpper
        LambdaQueryWrapper<Employee> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername,username);
        Employee emp=employeeService.getOne(lqw);

        if (emp==null){
            return R.error("登录用户名不存在！");
        }
        //3.验证MD5加密以后的结果  如果 MD5加密以后的结果一样  那么查看员工是不是禁止使用 如果加密结果不一样   那么登录失败
        if (!emp.getPassword().equals(MD5password)){
            return R.error("密码错误，登录失败！！");
        }
        //4.验证用户是否禁用 如果禁用登录失败  如果没有禁用登录成功
        if (emp.getStatus()==0){
            return R.error("员工禁用，登录失败！！");
        }

        request.getSession().setAttribute("Employee",emp.getId());
        return R.success(emp);
    }

    //退出方法  清理session中的数据就可以了
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("Employee");
        return R.success("退出成功！！");
    }

    //添加方法
    @PostMapping
    public R<String> add(@RequestBody Employee employee,HttpServletRequest servletRequest){

            //设置初始密码
            //密码做MD5加密
            String password=DigestUtils.md5DigestAsHex("123456".getBytes());
            employee.setPassword(password);

//            //设置创建时间
//            employee.setCreateTime(LocalDateTime.now());
//            employee.setUpdateTime(LocalDateTime.now());
//            log.info(employee.toString());

//            //设置创建人的id
//            String  emp = servletRequest.getSession().getAttribute("Employee").toString();
//            employee.setCreateUser(Long.valueOf(emp));
//            employee.setUpdateUser(Long.valueOf(emp));
            boolean save = employeeService.save(employee);
            return R.success("添加成功！！");

    }



    /*
    员工分页查询   get的请求方式
     */

//    @GetMapping("/page?page={page}&pageSize={pagesize}")
    @GetMapping("/page")
    public R<Page<Employee>> selectByPage(Integer page,Integer pageSize,String name){
        log.info("page:{},pageSize:{},name:{}",page,pageSize,name);
        Page<Employee> page1=new Page<>(page,pageSize);
        LambdaQueryWrapper<Employee> qw=new LambdaQueryWrapper<>();
        qw.like(!StringUtils.isEmpty(name),Employee::getName,name);
        qw.orderByDesc(Employee::getUpdateTime);
        Page<Employee> page2 = employeeService.page(page1,qw);
        if (page2==null){
            return R.error("查询失败！");
        }
        return R.success(page2);
    }

    /*
    更新员工
     */
    @PutMapping
    public R<String> modifyStatus(@RequestBody Employee employee,HttpServletRequest request){

        log.info(employee.toString());
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser((Long) request.getSession().getAttribute("Employee"));
        //直接修改
        boolean b = employeeService.updateById(employee);

        return R.success("修改状态成功！");
    }


    /*
    根据id查询员工
     */
    @GetMapping("/{id}")
    public R<Employee> selectById(@PathVariable Long id){
        log.info("需要修改的id是："+id.toString());
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }

}
